#include <stdio.h>
#include <conio.h>
int game1[15][15] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                     {1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1},
                     {1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3},
                     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int player_x=1,player_y=0;
void move_player(int dx, int dy) {
    int new_x = player_x + dx;
    int new_y = player_y + dy;
    // 不能超出
    if (new_x < 0 || new_x >= 15 || new_y < 0 || new_y >= 15) {
        return;
    }
    // 跟新后面的路将他变回0
    if (game1[new_x][new_y] == 0  ) {
        game1[player_x][player_y] = 0;
        player_x = new_x;
        player_y = new_y;
        game1[player_x][player_y] = 2;  // 更新小人位置
    }
    else if(game1[new_x][new_y] == 3)  //碰到出口就结束
    {
        printf("你赢啦!!!");
        fflush(stdout);
    }
    else {
        printf("不能穿过墙！\n");
    }
}

//打印地图
void print_maze() {
    for (int i = 0; i < 15; i++) {
        printf("\n");
        for (int j = 0; j < 15; j++) {
            if (game1[i][j] == 1) {
                printf("回");  // 墙
            } else if (game1[i][j] == 0) {
                printf("  ");  // 通路
            } else if (game1[i][j] == 3) {
                printf("E ");  // 出口
            }
            else if (game1[i][j] == 2) {
                printf("* ");  // 小人
            }
        }
    }
    printf("\n");
    fflush(stdout);
}
int main(){
    int bool = 1;
    int select = 0;

    do {
        printf("==========迷宫游戏==========\n"); //开始界面
        printf("         1.开始游戏\n");
        printf("         2.游戏说明\n");
        printf("         3.游戏难度\n");
        printf("         4.退出游戏\n");
        printf("===========================\n");
        fflush(stdout);
        scanf("%d",&select); //界面选择
        char key;//给你按键赋值
        switch(select) {
            case 1:
                print_maze();
                fflush(stdout);
                while (1) {
                    if (_kbhit()) {  // 检查是否有按键按下
                        key = _getch();  // 获取按下的键
                        if (key == 'w') {
                            move_player(-1,0);
                            print_maze();
                            fflush(stdout);
                        }
                        else if (key == 'a') {
                            move_player(0,-1);
                            print_maze();
                            fflush(stdout);
                        }
                        else if (key == 's') {
                            move_player(1,0);
                            print_maze();
                            fflush(stdout);
                        }
                        else if (key == 'd') {
                            move_player(0,1);
                            print_maze();
                            fflush(stdout);
                        }
                        else if (key == 27) {
                            break;
                        }
                    }
                }
                break;
            case 2:
                printf("===================================\n"); //游戏说明
                printf("感谢您体验本游戏，游戏的操作如下:\n");
                printf("1.将输入法调整为英文(小写)\n");
                printf("2.通过w,s, a, d四个键控制角色上下左右移动\n");
                printf("3.在任意界面均可按“ESC”键返回到主菜单\n");
                printf("胜利条件:移动角色到出口处，加油各位!\n");
                printf("====================================\n");
                fflush(stdout);
                while (1) {
                    if (_kbhit()) {  // 检查是否有按键按下
                        key = _getch();  // 获取按下的键
                        if (key == 27) {
                            break;
                        }
                    }
                }
            case 3:
                printf("难度就先算了\n");
                fflush(stdout);
                //按esc退出系统
                while (1) {
                    if (_kbhit()) {  // 检查是否有按键按下
                        key = _getch();  // 获取按下的键
                        if (key == 27) {
                            break;
                        }
                    }
                }
            case 4:
                bool = 0;
                break;
            default:
                break;
        }
    } while (bool);
    printf("程序退出，886");
    return 0;
}